package com.example.demo.controller;

import com.example.demo.api.R;
import com.example.demo.service.JdGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Shanks
 * @date 2020-05-26
 */
@RestController
public class JdGoodsController {

    @Resource
    private JdGoodsService jdGoodsService;

    @GetMapping("/parse/{keyword}")
    public R<String> parse(@PathVariable("keyword") String keyword) throws IOException {
        boolean b = jdGoodsService.initGoods(keyword);
        return R.success(String.format("Is init success: %s", b));
    }

    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    public R<List<Map<String, Object>>>  searchPage(@PathVariable("keyword") String keyword,
                                                @PathVariable("pageNo") Integer pageNo,
                                                @PathVariable("pageSize") Integer pageSize) throws IOException {
        return R.success(jdGoodsService.searchPage(keyword, pageNo, pageSize));
    }

    @GetMapping("/search/suggest")
    public R<List<String>> searchSuggest(@RequestParam(required = false, defaultValue = "") String keyword) {
        if (StringUtils.isBlank(keyword)) {
            return R.success(new ArrayList<>(Arrays.asList("Welcome", "Hello")));
        }
        return R.success(new ArrayList<>(Arrays.asList(keyword, keyword + "怎么样")));
    }
}